<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Lang;
use think\Response;
use app\common\library\ImageCraw;
use AlibabaCloud\SDK\Imageseg\V20191230\Imageseg;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Imageseg\V20191230\Models\SegmentHeadAdvanceRequest;
use AlibabaCloud\SDK\Facebody\V20191230\Models\AddFaceEntityRequest; //人脸样本添加
use AlibabaCloud\SDK\Facebody\V20191230\Models\AddFaceAdvanceRequest; //人脸数据添加
use AlibabaCloud\SDK\Facebody\V20191230\Models\SearchFaceAdvanceRequest;//人脸搜索
use AlibabaCloud\SDK\Facebody\V20191230\Models\DeleteFaceEntityRequest; //人脸样本删除
use AlibabaCloud\SDK\Facebody\V20191230\Facebody;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;
use think\Db;
/**
 * Ajax异步请求接口
 * @internal
 */
class Photo extends Frontend
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index(){
      /*$image = \think\Db::name('BroadcastImageAvatar')->where('status',0)->limit(100)->select();
      $this->assign('image',$image);
      return $this->view->fetch();*/
      $imagePhoto = Db::name('broadcastImagePhoto')
        ->alias('p')->join('__BROADCAST__ b','p.broadcast_id = b.id')->where('b.is_avatar',1)
        ->where('p.status',1)->where('p.avatar_id',0)->limit(1000)->field('p.id,p.broadcast_id,p.broadcast_image_id,p.image,p.avatar_id,p.status')->fetchSql(true)->select();
        dump($imagePhoto);
    }
    public function face(){
      $photo = [
        'id'=>25,
        'avatar'=>'/avatar/4_9_40_1.jpg',
      ];
      $path = ROOT_PATH.'public';
      $image = $path.$photo['avatar'];
      $entityId = 'P'.$photo['id'];

      $searchRlt = $this->SearchFaceAdvance($image);
      dump($searchRlt);
    //dump($searchRlt["Data"]['MatchList'][0]['QualitieScore']>85);
    }
    protected function SearchFaceAdvance($image){
      $config = get_addon_config('alioss');
      $aliConfig = new Config([
          // 必填，您的 AccessKey ID
          "accessKeyId" => $config['accessKeyId'],
          // 必填，您的 AccessKey Secret
          "accessKeySecret" => $config['accessKeySecret']
      ]);
      // 访问的域名
      $aliConfig->endpoint = "facebody.cn-shanghai.aliyuncs.com";
      $faceBody = new Facebody($aliConfig);
      $file = fopen($image, 'rb');
      // 场景二，使用任意可访问的url
      // $file = fopen('https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/facebody/AddFace/AddFace.png', 'rb');
      $stream = new Stream($file);
      $searchFaceAdvanceRequest = new SearchFaceAdvanceRequest([
          "dbName" => "default",
          "imageUrlObject" => $stream,
          "limit" => 1
      ]);
      $runtime = new RuntimeOptions([]);
      try {
          $resp = $faceBody->searchFaceAdvance($searchFaceAdvanceRequest, $runtime);
          $respon =json_decode(
              Utils::toJSONString($resp->body),true
          );
          $respon['code']='200';
          return $respon;
      } catch (\Exception $exception) {
          # 获取整体报错信息
          echo Utils::toJSONString($exception);
          # 获取单个字段
          $respon['code']='500';
          return $respon;
      }
    }
    protected function addFaceEntity($entityId){
      $config = get_addon_config('alioss');
      $aliConfig = new Config([
          // 必填，您的 AccessKey ID
          "accessKeyId" => $config['accessKeyId'],
          // 必填，您的 AccessKey Secret
          "accessKeySecret" => $config['accessKeySecret']
      ]);
      // 访问的域名
      $aliConfig->endpoint = "facebody.cn-shanghai.aliyuncs.com";
      $faceBody = new Facebody($aliConfig);
      $addFaceEntityRequest = new AddFaceEntityRequest([
          "dbName" => "default",
          "entityId" => $entityId,
      ]);
      $runtime = new RuntimeOptions([]);
      try {
          $resp = $faceBody->addFaceEntityWithOptions($addFaceEntityRequest, $runtime);
          $respon =json_decode(
            Utils::toJSONString($resp->body),true
        );
          $respon['code']='200';
          return $respon;
      } catch (\Exception $exception) {
          # 获取整体报错信息
          echo Utils::toJSONString($exception);
          # 获取单个字段
          $respon['code']='500';
          return $respon;
      }
    }
    protected function AddFaceAdvance($image,$entityId){
      $config = get_addon_config('alioss');
      $aliConfig = new Config([
          // 必填，您的 AccessKey ID
          "accessKeyId" => $config['accessKeyId'],
          // 必填，您的 AccessKey Secret
          "accessKeySecret" => $config['accessKeySecret']
      ]);
      // 访问的域名
      $aliConfig->endpoint = "facebody.cn-shanghai.aliyuncs.com";
      $faceBody = new Facebody($aliConfig);
      $file = fopen($image, 'rb');
      // 场景二，使用任意可访问的url
      // $file = fopen('https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/facebody/AddFace/AddFace.png', 'rb');
      $stream = new Stream($file);
      $addFaceAdvanceRequest = new AddFaceAdvanceRequest([
          "dbName" => "default",
          "imageUrlObject" => $stream,
          "entityId" => $entityId,
      ]);
      $runtime = new RuntimeOptions([]);
      try {
          $resp = $faceBody->addFaceAdvance($addFaceAdvanceRequest, $runtime);
          $respon =json_decode(
              Utils::toJSONString($resp->body),true
          );
          $respon['code']='200';
          return $respon;
      } catch (\Exception $exception) {
          # 获取整体报错信息
          echo Utils::toJSONString($exception);
          # 获取单个字段
          $respon['code']='500';
          return $respon;
      }
  }
}